import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import Home from '../views/Home/Home.vue'
import store from '../store/index';
import nProgress from 'nprogress';
import {RouteNameEnum, whiteRouteNameList} from "@/common-class/common-config/RouteClass";
import Login from "@/views/Login/Login.vue";
import R404 from "@/views/R404/R404.vue";
import PersonalCenter from "@/views/PersonalCenter/PersonalCenter.vue";
import DashBoard from "@/views/DashBoard/DashBoard.vue";
import ManagerIndex from "@/views/Manager/ManagerIndex.vue";
import TestIndex from "@/views/Test/TestIndex.vue";
const symbol404 = Symbol()
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: RouteNameEnum.home,
    component: Home,
    redirect: { name: RouteNameEnum.dashboard },
    children : [
      {
        path: 'personalCenter',
        name : RouteNameEnum.personalCenter,
        component: PersonalCenter,
      },
      {
        path: 'dashboard',
        name: RouteNameEnum.dashboard,
        component: DashBoard,
      },
      {
        path: 'manager',
        name: RouteNameEnum.manager,
        component: ManagerIndex
      },
      {
        path: 'test',
        name: RouteNameEnum.test,
        component: TestIndex
      }
    ]
  },
  {
    path: '/login',
    name: RouteNameEnum.login,
    component: Login
  },
  {
    path: '/404',
    name: symbol404,
    component: R404,
  },
  {
    path: '/:catchAll(.*)',
    redirect: {
      name: symbol404,
    },
  }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
const userTokenNotNull = (): boolean => {
  return store.getters['user/token'] && store.getters['user/token'] !== 'undefined' && store.getters['user/token'] !== 'null' && store.getters['user/token'] !== '';
};
router.beforeEach((to, from, next) => {
  nProgress.start();
  if (whiteRouteNameList.includes(to.name as string)) {
    next();
  } else {
    if (userTokenNotNull()) {
      next();
      // if (store.getters['user/permissions'].includes(to.name)) {
      //   next();
      // } else {
      //   next({ name: routeNameEnum.r401 });
      // }
    } else {
      next({ name: RouteNameEnum.login });
    }
  }
  nProgress.done();
});
export default router
